package com.yjb.demo.shardingjdbc.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjb.demo.shardingjdbc.common.ResponseResult;
import com.yjb.demo.shardingjdbc.domain.SysUser;
import com.yjb.demo.shardingjdbc.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description 
 * @author yinjinbiao
 * @create 2024/1/5 10:42
 * @version 1.0
 */
@RestController
@RequestMapping("/sysuser")
public class SysUserController {

	@Resource
	private SysUserService sysUserService;

/*	*//**
	 * 测试插入是否按照分库分表策略，插入到对应的库。
	 *
	 * 模拟当前年份为2024年，id模2为1，预期插入到 ds2024.sys_user_1
	 * @return
	 *//*
	@ApiOperation("单条插入测试")
	@GetMapping("/insert")
	public ResponseResult insert(){
		SysUser sysUser = new SysUser();
		sysUser.setId(1L);
		sysUser.setName("尹大");
		sysUser.setYear(2024);
		sysUserService.save(sysUser);
		return ResponseResult.ok();
	}*/

	/**
	 * 测试批量插入是否按照分库分表策略，插入到对应的库。
	 *
	 * 模拟10000条不同的数据,包括创建时间，插入到不同的表中。
	 * @return
	 */
	@ApiOperation("多条插入测试")
	@GetMapping("/insertBatch")
	public ResponseResult insertBatch(){

		List sysUserList = new ArrayList<SysUser>();

		Long tsStart = 1672542714000L;
		Long tsEnd = 1704683514000L;
		for (long i = 1; i <= 10000; i++) {
			SysUser sysUser = new SysUser();
			sysUser.setId(i);
			sysUser.setName(RandomUtil.randomString(2));
			sysUser.setYear(RandomUtil.randomEle(new Integer[]{2023, 2024}));
			sysUser.setCreateTime(new Date(RandomUtil.randomLong(tsStart, tsEnd)));
			sysUserList.add(sysUser);
		}
		sysUserService.saveBatch(sysUserList);
		return ResponseResult.ok();
	}

	/**
	 * 判断是否能够正确查询
	 * @param id
	 * @return
	 */
	@ApiOperation("单条查询测试")
	@GetMapping("/get/{id}")
	public ResponseResult get(@PathVariable Long id){
		return ResponseResult.ok(sysUserService.getById(id));
	}

	/**
	 * 查询id大于1的所有数据
	 * @return
	 */
	@ApiOperation("大于查询测试")
	@GetMapping("/getGt")
	public ResponseResult getGt(){
		return ResponseResult.ok(sysUserService.lambdaQuery().gt(SysUser::getId, 1L).list());
	}

	/**
	 * 查询id大于1的所有数据
	 * @return
	 */
	@ApiOperation("between测试")
	@GetMapping("/getBetween")
	public ResponseResult getBetween(){
		return ResponseResult.ok(sysUserService.lambdaQuery().between(SysUser::getId, 1L, 3L).list());
	}

	/**
	 * 根据分库字段年份查询数据
	 * @param year
	 * @return
	 */
	@ApiOperation("分库字段单条查询测试")
	@GetMapping("/getByYear/{year}")
	public ResponseResult getByYear(@PathVariable Integer year){
		return ResponseResult.ok(sysUserService.lambdaQuery().eq(SysUser::getYear, 2023).list());
	}

	/**
	 * 根据分库字段年份查询数据
	 * @return
	 */
	@ApiOperation("分库字段大于查询测试")
	@GetMapping("/getByYearGt")
	public ResponseResult getByYearGt(){
		return ResponseResult.ok(sysUserService.lambdaQuery().gt(SysUser::getYear, 2023).list());
	}

	/**
	 * 根据创建记录时间排序分页查询
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation("根据创建记录时间排序分页查询")
	@GetMapping("/pageByCreateTime")
	public ResponseResult page(int pageNo,int pageSize){
		Page<SysUser> page = Page.of(pageNo, pageSize);
		return ResponseResult.ok(sysUserService.page(page, new LambdaQueryWrapper<SysUser>().orderByAsc(SysUser::getCreateTime)));
	}
}
